import * as Cesium from '@cesium/Source/Cesium.js';

// https://www.cnblogs.com/onsummer/tag/Cesium/

const boxes = {};

//https://www.cnblogs.com/onsummer/p/14139575.html

boxes.add = (viewer, {
  id,
  positions,
  fabric
}) => {
  const extrudedPolygon = new Cesium.PolygonGeometry({
    polygonHierarchy: new Cesium.PolygonHierarchy(
      Cesium.Cartesian3.fromDegreesArray(positions)
    ),
    extrudedHeight: 30000
  });

  const instance = new Cesium.GeometryInstance({
    geometry: extrudedPolygon,
    id
  });

  const m = new Cesium.Material({
    fabric
  });

  const aper = new Cesium.MaterialAppearance({
    material: m
  });

  var p = viewer.scene.primitives.add(
    new Cesium.Primitive({
      geometryInstances: instance,
      appearance: aper,
      releaseGeometryInstances: false,
      compressVertices: false
    })
  );

  if (true) {

    // p.readyPromise.then(v => console.log(v));

    const vs = aper.vertexShaderSource;
    const fs = aper.fragmentShaderSource;
    const fs2 = aper.getFragmentShaderSource();

    console.log(`// 顶点着色器：${vs}`);
    console.log(`// 片元着色器：${fs}`);
    console.log(`// 片元着色器2：${fs2}`);
  }
}

boxes.addAll = (viewer) => {
  const opt = {
    id: "box-01",
    positions: [
      112.41726298378288 - 1,
      23.290411251106182,
      113.67072522399741 - 1,
      23.560312361463682,
      114.09370956893551 - 1,
      22.590768298743153,
      112.83803246418894 - 1,
      22.285610818885644
    ],
    fabric: {
      type: 'Color',
      uniforms: {
        color: new Cesium.Color(216 / 255.0, 170 / 255.0, 208 / 255.0).withAlpha(0.618)
      }
    }
  }
  boxes.add(viewer, opt);

  let id = "box-02";
  let d = 1;
  let positions = [
    112.41726298378288 + d,
    23.290411251106182,
    113.67072522399741 + d,
    23.560312361463682,
    114.09370956893551 + d,
    22.590768298743153,
    112.83803246418894 + d,
    22.285610818885644
  ];
  let fabric = { // 必须有函数 czm_getMaterial(czm_materialInput materialInput)
    source: `czm_material czm_getMaterial(czm_materialInput materialInput){
            	czm_material material = czm_getDefaultMaterial(materialInput);
              material.diffuse = vec3(0.8470588235294118, 0.66666666, 0.8156862745098039); //漫反射因子
             	return material;
            }`,
  }
  boxes.add(viewer, {
    id,
    positions,
    fabric
  });

  // 2.3. 顶点着色器与片元着色器
  // Material.prototype.shaderSource：可读可写：当前 material 对象的 source 属性，支持实时修改
  // Appearance.prototype.vertexShaderSource：只读：当前外观对象的顶点着色器，仅支持构造时传入
  // Appearance.prototype.fragmentShaderSource：只读：当前外观对象的片元着色器，仅支持构造时传入
  // Appearance.prototype.getFragmentShaderSource()：返回最终完全版片元着色器源代码。
  // 上面在 fabric 对象中的 source 属性指定的 glsl 源代码，与 console.log(m.shaderSource) 出来的是完全一样的，所以此处忽略。
  id = "box-03";
  d = 3;
  positions = [
    112.41726298378288 + d,
    23.290411251106182,
    113.67072522399741 + d,
    23.560312361463682,
    114.09370956893551 + d,
    22.590768298743153,
    112.83803246418894 + d,
    22.285610818885644
  ];
  fabric = { // 必须有函数 czm_getMaterial(czm_materialInput materialInput)
    source: `czm_material czm_getMaterial(czm_materialInput materialInput){
            	czm_material material = czm_getDefaultMaterial(materialInput);
  material.diffuse = vec3(0.8470588235294118, 0.66666666, 0.8156862745098039);
  material.specular = 1.0;
  material.shininess = 0.8;
             	return material;
            }`,
  }
  boxes.add(viewer, {
    id,
    positions,
    fabric
  });

  id = "box-04";
  d = 5;
  positions = [
    112.41726298378288 + d,
    23.290411251106182,
    113.67072522399741 + d,
    23.560312361463682,
    114.09370956893551 + d,
    22.590768298743153,
    112.83803246418894 + d,
    22.285610818885644
  ];
  fabric = {
    source: `czm_material czm_getMaterial(czm_materialInput materialInput){
       czm_material material = czm_getDefaultMaterial(materialInput);
       material.diffuse = vec3(0.24313725490196078, 0.7372549019607844, 0.9333333333333333);
       material.specular = 0.5;
       material.shininess = 0.8;
       material.emission = vec3(0.0, 0.66666666, 0.0);
              	return material;
            }`,
  }
  boxes.add(viewer, {
    id,
    positions,
    fabric
  });

  // 5. 给材质的着色器代码传入动态值：uniforms 的运用
  id = "box-05";
  d = 7;
  positions = [
    112.41726298378288 + d,
    23.290411251106182,
    113.67072522399741 + d,
    23.560312361463682,
    114.09370956893551 + d,
    22.590768298743153,
    112.83803246418894 + d,
    22.285610818885644
  ];
  fabric = {
    uniforms: {
      my_var: 0.8,
    },
    source: `
    czm_material czm_getMaterial(czm_materialInput materialInput)
    {
        czm_material material = czm_getDefaultMaterial(materialInput);
        material.diffuse = vec3(materialInput.st, 0.0);
        material.alpha = my_var;
        return material;
    }
    `
  }
  boxes.add(viewer, {
    id,
    positions,
    fabric
  });

  boxes.addMove3D(viewer);

}

// https://www.cnblogs.com/onsummer/p/14212009.html
boxes.addMove3D = function(viewer) {
  const extrudedPolygon = new Cesium.PolygonGeometry({
    polygonHierarchy: new Cesium.PolygonHierarchy(
      Cesium.Cartesian3.fromDegreesArray([
        112.41726298378288, 23.290411251106182,
        113.67072522399741, 23.560312361463682,
        114.09370956893551, 22.590768298743153,
        112.83803246418894, 22.285610818885644
      ])
    ),
    extrudedHeight: 30000
  });

  const instance = new Cesium.GeometryInstance({
    geometry: extrudedPolygon,
    id: 'Position3D'
  });

  const m = new Cesium.Material({
    fabric: {
      type: 'Color',
      uniforms: {
        color: new Cesium.Color(216 / 255.0, 170 / 255.0, 208 / 255.0).withAlpha(0.618),
      },
    }
  });
// 3. 坐标陷阱：模型坐标系≠东北上坐标系
  const aper = new Cesium.MaterialAppearance({
    fragmentShaderSource: ` varying vec3 v_positionEC;
      varying vec3 v_normalEC;
      varying vec2 v_st;

      void main()
      {
          vec3 positionToEyeEC = -v_positionEC;

          vec3 normalEC = normalize(v_normalEC);
        #ifdef FACE_FORWARD
          normalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC);
        #endif

          czm_materialInput materialInput;
          materialInput.normalEC = normalEC;
          materialInput.positionToEyeEC = positionToEyeEC;
          materialInput.st = v_st;
          czm_material material = czm_getMaterial(materialInput);
          material.diffuse = vec3(0.24313725490196078, 0.7372549019607844, 0.9333333333333333);
          material.emission = vec3(0.0, 0.66666666, 0.0);
          material.specular = 0.5;
          material.shininess = 0.8;

        #ifdef FLAT
          gl_FragColor = vec4(material.diffuse + material.emission, material.alpha);
        #else
          gl_FragColor = czm_phong(normalize(positionToEyeEC), material, czm_lightDirectionEC);
        #endif
      }
     `,
    vertexShaderSource: `
      attribute vec3 position3DHigh;
      attribute vec3 position3DLow;
      attribute vec3 normal;
      attribute vec2 st;
      attribute float batchId;

      varying vec3 v_positionEC;
      varying vec3 v_normalEC;
      varying vec2 v_st;

      void main()
      {
          vec4 p = czm_computePosition();

          v_positionEC = (czm_modelViewRelativeToEye * p).xyz;      // position in eye coordinates
          v_normalEC = czm_normal * normal;                         // normal in eye coordinates
          v_st = st;
          vec4 positionMC = czm_inverseModelView * vec4(v_positionEC, 1.0);
          vec4 positionMC_new = vec4(positionMC.xy, positionMC.z + czm_frameNumber * 200.0, 1.0); // z轴向上平移动画
          vec4 resultPosition = czm_modelViewInfiniteProjection * positionMC_new; // 一步直接算到 gl_Position 所需的坐标
          gl_Position = resultPosition;

      }
      `,
  });

  var p = viewer.scene.primitives.add(new Cesium.Primitive({
    geometryInstances: instance,
    appearance: aper,
    releaseGeometryInstances: false,
    compressVertices: false,
  }));
}


// Fabric —— 材质JSON规范
// https://www.cnblogs.com/onsummer/p/14059215.html

boxes.addJsonFabric = function(viewer) {
  
}


export default boxes;
